import { useEffect } from "react";
import { useSelector } from "react-redux";
import { useHistory, useLocation } from "react-router";

function useGuards() {
    const { user } = useSelector((state) => state.login);
    const { replace } = useHistory();
    const { pathname } = useLocation();

    useEffect(() => {
        if (!user) {
            localStorage.setItem("prevpath", pathname);
            replace("/login");
        }
    }, [user]);
}

export { useGuards };
